package com.yeswater.redis7.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Description:
 * @author： yeswater
 * @create： 2024/6/27
 */

@Service
@Slf4j
public class HyperLogLogService {
	@Resource
	private RedisTemplate redisTemplate;
	
	/**
	 * 模拟后台有用户点击首页，每个用户来自不同ip地址
	 */
	@PostConstruct
	public void init() {
		log.info("------模拟后台有用户点击首页，每个用户来自不同ip地址");
		new Thread(() -> {
			String ip = null;
			for (int i = 1; i <= 200; i++) {
				Random r = new Random();    
				ip = r.nextInt(256) + "." + r.nextInt(256) + "." + r.nextInt(256) + "." + r.nextInt(256);
				
				Long hll = redisTemplate.opsForHyperLogLog().add("hll", ip);
				log.info("ip={},该ip地址访问首页的次数={}", ip, hll);
				//暂停几秒钟线程
				try {
					TimeUnit.SECONDS.sleep(3);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}, "t1").start();
	}
	
}
